Removes trailing zeros after the decimal point in a string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
Input: String to remove zeros from. |
||
character(len=*), | intent(out) | :: | result |
Output: String without trailing zeros. |
subroutine remove_decimal_zeros(str, result) !! Removes trailing zeros after the decimal point in a string. character(len=*), intent(in) :: str !! Input: String to remove zeros from. character(len=*), intent(out) :: result !! Output: String without trailing zeros. integer :: i, len_str !! Loop counter and string length. len_str = len_trim(str) result = adjustl(str(1:len_str)) ! Find the position of the decimal point i = index(result, '.') ! If there's a decimal point, remove trailing zeros if (i > 0) then do while (len_str > i .and. result(len_str:len_str) == '0') len_str = len_str - 1 end do if (result(len_str:len_str) == '.') len_str = len_str - 1 result = result(1:len_str) end if end subroutine remove_decimal_zeros